package com.leyou.user.service.impl;

import com.leyou.common.enums.LyExceptionEnums;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.NumberUtils;
import com.leyou.user.entity.User;
import com.leyou.user.mapper.UserMapper;
import com.leyou.user.properties.UserProperties;
import com.leyou.user.service.UserService;
import com.leyou.user.utils.CodecUtils;
import com.netflix.discovery.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.apache.bcel.classfile.Code;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author linzx
 * @date 2020/3/29
 */
@Service
@EnableConfigurationProperties(UserProperties.class)
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AmqpTemplate amqpTemplate;
    @Autowired
    private StringRedisTemplate redisTemplate;

   @Autowired
   private UserProperties userProps;

    @Override
    public Boolean checkData(String data, Integer type) {

        User user = new User();

        switch (type) {
            case 1:
                user.setUsername(data);
                break;
            case 2:
                user.setPhone(data);
                break;
            default:
                throw new LyException(LyExceptionEnums.INVALID_PARAM);
        }
        int count = userMapper.selectCount(user);
        return count == 0;
    }

    @Override
    public void sendCode(String phone) {
        String key = userProps.getphonePrefix() + phone ;
        String code = NumberUtils.generateCode(6);
        Map<String,String> msg = new HashMap<>();
        msg.put("phone",phone);
        msg.put("code",code);
        amqpTemplate.convertAndSend(userProps.getUserExchangeForCode(),userProps.getUserRoutingkeyForCode(),msg);
        redisTemplate.opsForValue().set(key,code,userProps.getcheckcodeLiveInMinutes(), TimeUnit.MINUTES);
    }

    @Override
    @Transactional
    public void register(User user, String code) {
        String cacheCode = redisTemplate.opsForValue().get(userProps.getphonePrefix() + user.getPhone());
        if (!StringUtils.equals(code,cacheCode)) {
            throw new LyException(LyExceptionEnums.INVALID_VERIFY_CODE);
        }
        String salt = CodecUtils.generateSalt();
        user.setSalt(salt);
       user.setPassword( CodecUtils.md5Hex(user.getPassword(),salt));
       user.setCreated(new Date());
       userMapper.insert(user);
    }

    @Override
    public User login(String username, String password) {
        User user = new User();
        user.setUsername(username);
        User one = userMapper.selectOne(user);
        if (one == null) {
            throw new LyException(LyExceptionEnums.INVALID_USERNAME_PASSWORD);
        }
        if (!StringUtils.equals(one.getPassword(), CodecUtils.md5Hex(password,one.getSalt()))) {
            throw new LyException(LyExceptionEnums.INVALID_USERNAME_PASSWORD);
        }
        return one;
    }
}
